/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.isi.pegasus.planner.subworkflow.scheduler.implementation;

import edu.isi.pegasus.planner.classes.PegasusBag;
import edu.isi.pegasus.planner.dax.ADAG;
import edu.isi.pegasus.planner.subworkflow.estimator.EstimatorFactory;
import edu.isi.pegasus.planner.subworkflow.estimator.EstimatorImplementation;
import edu.isi.pegasus.planner.subworkflow.partitioner.DAXAbstraction;
import edu.isi.pegasus.planner.subworkflow.partitioner.SiteAbstraction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author wchen
 *
 **/

  class writeToFile implements Runnable
{
     private ADAG dax;
     Thread runner;
     private boolean done;
     private String daxPath;
     public void getDone()
     {
         while(!done){
             try
             {
                Thread.sleep(10000);
             }
             catch (Exception e)
             {
                 e.printStackTrace();
             }

         }
     }
     public writeToFile(ADAG dax, String daxPath)
     {
         runner = new  Thread(this, "");
        this.dax = dax;
        this.daxPath = daxPath;
        this.done   = false;
        runner.start();
     }
    public void run()
    {
        dax.writeToFile(daxPath);
        this.done = true;
    }
}

   class estimatorThread implements Runnable
{
     private ADAG dax;
     Thread runner;
     private boolean done;
     private String daxPath;
     private DAXAbstraction bigDax;
     private SiteAbstraction site;
     private PegasusBag mBag;
     private Map mJob2Time;
     private String className;
     private ArrayList mSiteIndex;
     private Map map;


     public void getDone()
     {
         while(!done){
             try
             {
                 Thread.sleep(10000);
             }
             catch (Exception e)
             {
                 e.printStackTrace();
             }

         }
     }
     public estimatorThread(DAXAbstraction bigDax, SiteAbstraction site,
             PegasusBag mBag, Map mJob2Time, String className,
             ArrayList mSiteIndex, Map map)
     {
         runner = new  Thread(this, "");

        this.done   = false;
        this.bigDax = bigDax;
        this.site   = site;
        this.mBag   = mBag;
        this.mJob2Time  = mJob2Time;
        this.className  = className;
        this.mSiteIndex = mSiteIndex;
        this.map        = map;
        runner.start();
     }
    public void run()
    {
        EstimatorImplementation estimator =
        EstimatorFactory.loadInstance(bigDax, site, bigDax.subDag, mBag, (HashMap)mJob2Time, className);
        //Map don't worry, its private
        //site is null
        //bigDax is private
        //mJob2Time is only read
        for(int i = 0; i < mSiteIndex.size(); i++)
        {
            SiteAbstraction siteA = (SiteAbstraction)mSiteIndex.get(i);
            double runtime = estimator.getEstimatedRuntime(siteA);
            
            System.out.println("for dax " + bigDax.adag.getName() + " runtime: " + runtime );
            if(!map.containsKey(siteA))
                map.put(siteA, runtime);
        }
        this.done = true;
    }
}


 

public class ParallelThread{
    public static void main(String[] args)
    {
        Map map = new HashMap<String, Double>();
        map.put("A", 1.1);
        map.put("B", 2.2);
        //getSizeMapThread thread1 = new getSizeMapThread(0,2,map,"");
        //thread1.runner.start();
        //double sum  = thread1.getSum();
        try{
            Thread.currentThread().sleep(1000);
        }catch (Exception e)
        {

        }
        //System.out.println(sum);
    }

}